package com.syqc.monitor.ui.stopcar

import android.view.View
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.syqc.comlib.base.BaseToolbarRecycleActivity
import com.syqc.comlib.utils.ToolbarHelper
import com.syqc.comlib.utils.startActivityTo
import com.syqc.comlib.utils.toast
import com.syqc.entity.StopStats
import com.syqc.monitor.R
import com.syqc.monitor.databinding.HeadStopMileBinding
import com.syqc.utils.DataUtil
import com.syqc.utils.Keys
import com.syqc.utils.ToolsUtil

/**
 *@Author Jasper
 *@Time   2020/12/11
 *@Desc   停车统计列表
 */
class StopCarInfoActivity : BaseToolbarRecycleActivity() {
    private var title = ""
    private val mAdapter by lazy { StopCarInfoAdapter() }

    private lateinit var time: String
    private lateinit var long: String
    private lateinit var total: String
    private var infoEntity: MutableList<StopStats.StopInfo>? = null
    override fun initView() {

        title = intent.getStringExtra(Keys.INTENT_TITLE) ?: ""

        time = intent.getStringExtra(Keys.INTENT_TIME) ?: ""
        ToolbarHelper.setToolbar(this, title)
        DataUtil.stopCar?.let {
            long = it.long
            total = it.total
            infoEntity = it.list
        }
        super.initView()
    }

    override fun initRecyclerView() {
        mRecycler.adapter = mAdapter
        mAdapter.setList(infoEntity)
        mAdapter.addHeaderView(initHeadView())
    }

    override fun initHeadView(): View {
        val binding = HeadStopMileBinding.inflate(layoutInflater)
        binding.includeStopMile.tvInfoValue.setText(R.string.desc_com_num)
        binding.lrTvInfoLong.setLeftTitle(R.string.desc_alarm_info_long_times)
        binding.lrTvInfoTime.setRightText(time)
        binding.lrTvInfoLong.setRightText("$long / $total ${getString(R.string.unit_num)}")
        return binding.root
    }

    override fun setListener() {
        mAdapter.setOnItemClickListener { adapter, view, position ->
            if (ToolsUtil.isFastClick()) return@setOnItemClickListener
            val infoEntity = mAdapter.data[position]
            if (infoEntity.total == 0) {
                toast(R.string.desc_no_about_data)
                return@setOnItemClickListener
            }
            startActivityTo<StopCarDetailActivity>(this) {
                putExtra(
                    Keys.INTENT_TITLE,
                    "$title-(${infoEntity.total})${getString(R.string.unit_num)}"
                )
                putParcelableArrayListExtra(Keys.INTENT_DATA, infoEntity.list)
            }
        }
        binding.tvToTop.setOnClickListener {
            mRecycler.smoothScrollToPosition(0)
        }

        mRecycler.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)
                val position = mLayoutManger.findFirstVisibleItemPosition()
                binding.tvToTop.isVisible = position > 0
            }
        })
    }


    inner class StopCarInfoAdapter :
        BaseQuickAdapter<StopStats.StopInfo, BaseViewHolder>(R.layout.item_day_mile) {
        //        private val num by lazy {getString(R.string.unit_num)}
        override fun convert(holder: BaseViewHolder, item: StopStats.StopInfo) {
            item.run {
                val position = holder.layoutPosition
//                val num = if (position < 10) "0$position" else "$position"
                holder.setText(R.id.tvInfoOrder, position.toString())
                    .setText(R.id.tvInfoDateType, name)
                    .setText(R.id.tvInfoValue, "$total")

                holder.getView<TextView>(R.id.tvInfoValue)
                    .setTextColor(ContextCompat.getColor(context, R.color.color_246))
            }
        }

    }
}